#include "stdafx.h"
#include "LitEditorSettingsTabWidget.h"

namespace Lit
{
	namespace Editor
	{
		void SettingsTableWidget::_itemChanged( const QString &name, const QString & val )
		{
			mItems.updateValue( FROM_QT(name), FROM_QT(val) );
			emit( itemChanged( mName, FROM_QT(name), FROM_QT(val) ) );
		}
		SettingsTableWidget::SettingsTableWidget( QWidget * parent, const std::string &name ) : QWidget( parent ), mName(name)
		{
			mUi.setupUi(this);
			mUi.addPropertyPanelButton->setChecked( false );
		}
		SettingsTableWidget::~SettingsTableWidget(){}

		SettingsMap& SettingsTableWidget::getSettings(){ return mItems; }
		const SettingsMap& SettingsTableWidget::getSettings() const{ return mItems; }

		const std::string &SettingsTableWidget::getName() const{return mName; }

		const Ui::SettingsTableWidget& SettingsTableWidget::getUi() const{ return mUi; }
		Ui::SettingsTableWidget& SettingsTableWidget::getUi(){ return mUi; }

		SettingsItemWidget* SettingsTableWidget::_createSettingByType( const SettingsValue& val )
		{
			SettingsItemWidget* ret=NULL;
			switch( val.third )
			{
			case ST_BOOL: ret = new SettingsBoolWidget( Ogre::StringConverter::parseBool(val.second), this ); break;
			case ST_DOUBLE:  
			case ST_FLOAT:  ret = new SettingsDoubleWidget( -1000.0, 1000.0, Ogre::StringConverter::parseReal(val.second), this ); break;
			case ST_INT:  ret = new SettingsIntWidget( 0, 100000, Ogre::StringConverter::parseInt(val.second), this ); break;
			case ST_MATRIX3:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_MATRIX4:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_QUATERNION :  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_STRING:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_VECTOR2:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_VECTOR3:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_VECTOR4:  ret = new SettingsTextWidget( val.second, this ); break;
			case ST_COLOR:  ret = new SettingsColorWidget( Ogre::StringConverter::parseColourValue(val.second), this ); break;
			case ST_STRINGLIST:  ret = new SettingsTextWidget( val.second, this ); break;
			}
			mWidgets[val.first] = ret;
			return ret;
		}
		void SettingsTableWidget::setupUi( const SettingsMap& settings )
		{
			mUi.tableWidget->clearContents();
			for( auto i=mWidgets.begin(); i!=mWidgets.end(); i++ )
				if( i->second ){ delete i->second, i->second=NULL; }
			mWidgets.clear();
			mUi.retranslateUi(this);
			mUi.tableWidget->setRowCount( settings.size() );
			mItems = settings;
			size_t row = 0;
			for( auto i=settings.begin(); i!=settings.end(); i++ )
			{
				SettingsItemWidget * itm2 = _createSettingByType( i->second );
				mUi.tableWidget->setItem( row, 0, new QTableWidgetItem(TO_QT(i->first)) );
				if( itm2 )
				{
					itm2->setName( i->first );
					connect(itm2, SIGNAL(itemChanged( const QString &, const QString& )), this, SLOT( _itemChanged( const QString&, const QString & ) ) );
					mUi.tableWidget->setCellWidget( row, 1, itm2 );
					mUi.tableWidget->setRowHeight( row, itm2->sizeHint().height() );
				}
				row++;
			}
		}
		SettingsTabWidget::SettingsTabWidget( QWidget * parent ) : QTabWidget(parent){ }
		SettingsTabWidget::~SettingsTabWidget( ){}

		SettingsTableWidget* SettingsTabWidget::addPage( const std::string &name )
		{
			SettingsTableWidget * w = new SettingsTableWidget( this, name );
			QTabWidget::addTab( w, TO_QT(name) );
			return w;
		}
		void SettingsTabWidget::clearPages( )
		{
			QTabWidget::clear();
			mPages.clear();
		}
		void SettingsTabWidget::removePage( const std::string &name )
		{
			for( auto i=mPages.begin(); i!=mPages.end(); i++ )
				if( i->second->getName() == name )
				{
					delete (i->second), i->second = NULL;
					mPages.erase( i );
					return;
				}
		}
	};
};